# -*- encoding: utf-8 -*-

ROOT_DIR = File.expand_path('../..', __FILE__) unless defined?(ROOT_DIR)
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'rubygems' unless defined?(Gem)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
Bundler.require(:default, 'development')
$LOAD_PATH << 'lib'
Oj.mimic_JSON

require 'faye/cors'
require 'faye/client'

ENV['MONGO_URL'] ||= 'mongodb://sandbox:sandbox@d.b2b2c.in/sandbox_devel'
ENV['REDIS_URL'] ||= 'redis://:sandbox@r.b2b2c.in:6379/0'

redis_opts = { url: ENV['REDIS_URL'], driver: :hiredis }

# 任务队列
def current_redis(space, opts)
  Redis::Namespace.new( space, redis: Redis.new( opts ) )
end

#（动态）定时任务
Sidekiq::Scheduler.dynamic = true
redis_conn = proc { current_redis(:sidekiq, redis_opts) }

# 客户端
Sidekiq.configure_client do |c|
  c.redis = ConnectionPool.new(size: 5, &redis_conn)
end
puts 'Sidekiq 设置完毕。'
puts '_'*88

ENV['GENGHIS_SERVERS'] = ENV['MONGO_URL']
puts 'Genghis 设置完毕。'
puts '_'*88

App = Faye::RackAdapter.new(
  Sinatra::Application,
  mount: '/faye',
  timeout: 25,
  engine: {
    type: Faye::Redis,
    uri: ENV['REDIS_URL'],
    namespace: 'objects'
  },
  extensions: [ Faye::Cors.new ],
  ping: 25
)
puts 'Faye 设置完毕。'
puts '_'*88